<?php
declare (strict_types = 1);

namespace app\validate\common\user;

use app\model\communal\ImMessageModel;
use think\Validate;

class ChatValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'id' => ['require', 'number', 'gt:0'],
        'expert_id' => ['require', 'number', 'gt:0'],
        'user_id' => ['require', 'number', 'gt:0'],
        'sender_id' => ['require', 'number', 'gt:0'],
        'receiver_id' => ['require', 'number', 'gt:0'],
        'content' => ['max:500'],
        'sender_type' => ['require', 'in:1,2'],
        'receiver_type' => ['require', 'in:1,2'],
        'user_type' => ['require', 'in:1,2'],
        'type' => ['require'],
        'state' => ['require', 'in:1,2,3'],

        'page' => ['number', 'gt:0'],
        'page_size' => ['number', 'gt:0'],
    ];

    protected $field = [
        'content' => '内容',
        'expert_id' => '专家',
        'user_id' => '参与人',
        'sender_id' => '发送人',
        'receiver_id' => '接收人',
        'user_type' => '参与人类型',
        'sender_type' => '发送人类型',
        'receiver_type' => '接收人类型',
        'type' => '消息类型',
        'state' => '消息状态',

        'page' => '当前页数',
        'page_size' => '每页条数',
        'orderBy' => '排序字段',
        'orderDir' => '排序规则',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        'sender_id.gt' => ':attribute不存在',
        'user_id.gt' => ':attribute不存在',
        'receiver_id.gt' => ':attribute不存在',
    ];

    /**
     * 场景
     * @var array
     */
    protected $scene = [
        'index' => ['page', 'page_size', 'orderBy', 'orderDir', 'user_id', 'user_type'],
        'friends' => ['page', 'page_size'],
        'company' => ['user_id'],
        'admin' => ['company_id', 'user_id'],
        'already' => ['sender_id', 'sender_type'],
        'chat' => ['expert_id'],
        'customer' => ['expert_id'],
        'type' => ['id', 'type'],
    ];

    /**
     * scene save
     *
     * @return $this
     */
    public function sceneSend()
    {
        return $this->only(ImMessageModel::$saveColumn);
    }

    /**
     * scene update
     *
     * @return $this
     */
    public function sceneUpdate()
    {
        return $this->only(ImMessageModel::$updateColumn);
    }

    /**
     * scene update
     *
     * @return $this
     */
    public function sceneItem()
    {
        return $this->only(['id', 'title'])
            ->remove('title', 'require')
            ->append('title', 'checkTitleUnique:update');
    }
}
